﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MGXAPI.Filter
{
    /// <summary>
    /// 统一登陆验证
    /// https://blog.csdn.net/sat472291519/article/details/12036699
    /// </summary>
    public class CheckLoginAttribute : ActionFilterAttribute
    { 
        //重写ActionFilterAttribute中的OnActionExecuted方法，表示在执行Action之前执行此方法
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            //判断Action方法的Control是否跳过登录验证
            if (filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(SkipCheckLoginAttribute), false))
            {
                return;
            }
            //判断Action方法是否跳过登录验证
            if (filterContext.ActionDescriptor.IsDefined(typeof(SkipCheckLoginAttribute), false))
            {
                return;
            }
            if (filterContext.HttpContext.Session["User"] == null)
            {
                //跳转方法1：
                //filterContext.HttpContext.Response.Redirect("/News/Login");
                //跳转方法2：
                ViewResult view = new ViewResult();
                //指定要返回的完整视图名称
                view.ViewName = "~/View/News/Login.cshtml";
            }
        }
    }
}